﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace YanForum.Engine.Services
{
    public static class Comment
    {
        static string connectionString = BaseService.ConnectionString;

        //by Tuan
        public static void InsertComment(int userId, int commentId, int contentId, string commentPost)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("InsertComment", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@userId", userId);
                cmd.Parameters.AddWithValue("@contentId", contentId);
                cmd.Parameters.AddWithValue("@commentId", commentId);
                cmd.Parameters.AddWithValue("@commentPost", commentPost);
                cmd.Parameters.AddWithValue("@dateNow", DateTime.Now);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

        //By Tuan
        public static YanForum.Engine.ViewModels.CommentIndex GetListCommentByContentId(int contentId, int pageSize, int pageIndex)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("GetListCommentByContentId", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@contentId", contentId);
                cmd.Parameters.AddWithValue("@pageSize", pageSize);
                cmd.Parameters.AddWithValue("@pageIndex", pageIndex);

                var reader = cmd.ExecuteReader();

                var model = new YanForum.Engine.ViewModels.CommentIndex();

                reader.Read();
                int total = (int)reader["TotalCount"];
                model.TotalCount = total;

                // gets the second
                reader.NextResult();
                var lstComment = new List<ViewModels.CommentItem>();
                while (reader.Read())
                {
                    var fullName = reader["FullName"].ToString();
                    var userName = reader["UserName"].ToString();

                    var item = new ViewModels.CommentItem
                    {
                        Id = (int)reader["Id"],
                        UserId = int.Parse(reader["UserId"].ToString()),
                        UserName = userName,
                        FullName = !string.IsNullOrEmpty(fullName) ? fullName : userName,
                        TotalSubComment = int.Parse(reader["TotalSubComment"].ToString()),
                        Folder = reader["Folder"].ToString(),
                        Avatar = reader["Avatar"].ToString(),
                        CreateDate = DateTime.Parse(reader["CreateDate"].ToString()),
                        CommentPost = reader["CommentPost"].ToString()
                    };

                    lstComment.Add(item);
                }
                model.ListComment = lstComment;              

                reader.Close();

                return model;
            }
        }

        //By Tuan
        public static List<YanForum.Engine.ViewModels.CommentItem> GetListSubComment(int commentId)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("GetListSubComment", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@commentId", commentId);

                var reader = cmd.ExecuteReader();

                var lstSubComment = new List<ViewModels.CommentItem>();

                while (reader.Read())
                {
                    var fullName = reader["FullName"].ToString();
                    var userName = reader["UserName"].ToString();
                    var item = new ViewModels.CommentItem
                    {
                        Id = (int)reader["Id"],
                        UserId = int.Parse(reader["UserId"].ToString()),
                        UserName = userName,
                        FullName = !string.IsNullOrEmpty(fullName) ? fullName : userName,
                        Folder = reader["Folder"].ToString(),
                        Avatar = reader["Avatar"].ToString(),
                        CreateDate = DateTime.Parse(reader["CreateDate"].ToString()),
                        CommentPost = reader["CommentPost"].ToString()
                    };

                    lstSubComment.Add(item);
                }
                reader.Close();

                return lstSubComment;
            }
        }

        //By Tuan
        public static YanForum.Engine.ViewModels.ManageCommentIndex GetManageListComment(YanForum.Engine.ViewModels.ManageCommentOptions options)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                DateTime createDate;
                DateTime? temp;
                temp = DateTime.TryParse(options.CreateDate,out createDate) ? createDate :(DateTime?) null;

                conn.Open();
                SqlCommand cmd = new SqlCommand("GetManageListComment", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@search", options.Search);
                cmd.Parameters.AddWithValue("@createDate", temp);
                cmd.Parameters.AddWithValue("@approved", options.Approved ? 1 : 0);
                cmd.Parameters.AddWithValue("@userName", options.UserName);
                cmd.Parameters.AddWithValue("@pageSize", options.PageSize);
                cmd.Parameters.AddWithValue("@pageIndex", options.PageIndex);

                var reader = cmd.ExecuteReader();

                var model = new YanForum.Engine.ViewModels.ManageCommentIndex();
                model.Options = options;

                reader.Read();
                int total = (int)reader["TotalCount"];
                model.TotalCount = total;

                // gets the second
                reader.NextResult();

                var lstComment = new List<ViewModels.ManageCommentItem>();
                while (reader.Read())
                {
                    var fullName = reader["FullName"].ToString();
                    var userName = reader["UserName"].ToString();
                    var item = new ViewModels.ManageCommentItem
                    {
                        Id = (int)reader["Id"],
                        UserId = int.Parse(reader["UserId"].ToString()),
                        UserName = userName,
                        Approved = Boolean.Parse(reader["Approved"].ToString()),
                        ContentId = Int32.Parse(reader["ContentId"].ToString()),
                        FullName = !string.IsNullOrEmpty(fullName) ? fullName : userName,
                        CreateDate = DateTime.Parse(reader["CreateDate"].ToString()),
                        CommentPost = reader["CommentPost"].ToString()
                    };

                    lstComment.Add(item);
                }
                model.ListComment = lstComment;
                reader.Close();

                return model;
            }
        }

        //By Tuan
        public static void CommentDelete(string Ids, int userId)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("CommentDelete", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@Ids", Ids);
                cmd.Parameters.AddWithValue("@userId", userId);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

        //By Tuan
        public static void CommentApproved(string Ids, int userId)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("CommentApproved", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@Ids", Ids);
                cmd.Parameters.AddWithValue("@userId", userId);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

        //By Tuan
        public static int CountAllComemntByApproved(short approved)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("CountAllComemntByApproved", conn);

                cmd.Parameters.AddWithValue("@approved", approved);
                cmd.CommandType = CommandType.StoredProcedure;
                var reader = cmd.ExecuteReader();
                int totalCount = 0;

                while (reader.Read())
                {
                    totalCount = (int)reader["TotalCount"];
                }
                reader.Close();

                return totalCount;
            }
        }
    }
}
